package com.erju.schedule;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.erju.dao.LabAppointmentMapper;
import com.erju.pojo.SlLabAppointment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;


/**
 * @Description: 预约信息管理定时任务
 * @author: erju
 * @date: 2022.05.05
 */
@Slf4j
@Component
public class AppointmentSchedule {
    /**
     * 定时将数据库中的预约过期信息设置为过期
     */
    @Scheduled(cron = "0 0 0 1/1 * ?")
    //@Scheduled(fixedDelay = 30 * 1000)
    private void resetAppointmentFlag() {
        log.info("开始执行定时器任务----------------");
        //获取当天开始的时间
        long beginOfDay = DateUtil.beginOfDay(new Date()).getTime();
        //引入mapper
        LabAppointmentMapper appointmentMapper = SpringUtil.getBean(LabAppointmentMapper.class);
        List<SlLabAppointment> slLabAppointments = appointmentMapper.selectNoOverdueAppointment();
        //过滤更新
        slLabAppointments.stream().filter(slLabAppointment -> slLabAppointment.getAppointment_time().getTime() < beginOfDay
        ).forEach(slLabAppointment -> {
            slLabAppointment.setAppointment_flag(4);
            appointmentMapper.updateAppointmentFlag(slLabAppointment);
            log.info(slLabAppointment.toString());
        });
    }
}
